package demo3;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-05-28
 * Time: 21:51
 */
// 写一个泛型类，其中有个方法，然后求数组中的最大值

class Alg <T extends Comparable<T>>{
    public T find_max(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }

}
// 静态方法
class Alg2 {
    public static <T extends Comparable<T>> T find_max (T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

public class Test {
    public static void main(String[] args) {
        // 静态泛型方法的使用
        Integer[] array = {1,0,9,3,5,8,4};
        Integer max = Alg2.<Integer>find_max(array);
    }

    public static void main1(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {1,0,9,3,5,8,4};
        System.out.println(alg.find_max(array));
    }
}
